import request from "@/utils/request";
import BASE_URL from '@/utils/env'
/**
 * 登录API函数
 * 该函数用于向服务器发送用户登录请求
 *
 * @param {Object} data - 包含用户登录信息的对象，通常包括用户名和密码
 * @returns {Promise} - 返回一个Promise对象，用于处理异步请求
 */
export const loginApi = (data) => request({
  url: 'user/login', // 登录接口的URL
  method: 'post', // 使用POST方法发送请求
  data // 将用户登录信息作为请求体发送
})

export const registerApi = data => request({
  url: 'user/register',
  method: 'post',
  data
})

export const getHomeNoticesApi = () => request({
  url: 'notice/query'
})

export const getHomeHotOrdersApi = () => request({
  url: 'order/query',
  data: {
    pageNum: 1,
    pageSize: 2,
    state: 0,
    isHome: true
  }
})

export const getOrderListApi = data => request({
  url: 'order/query',
  data
})

// 上传文件

export const uploadFileApi = (filePath,fileType) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: BASE_URL + 'upload',
      header: {
        Authorization: uni.getStorageSync('token')
      },
      filePath: filePath,
      name: 'file',
      formData: {
        fileType: fileType
      },
      success: (uploadFileRes) => {
        const result = JSON.parse(uploadFileRes.data)
        if(result.code === 1) {
          resolve(result.data)
        } else if(result.code === 401) {
          uni.navigateTo({url: '/pages/login/login'})
        }
      },
      fail: (err) => {
        uni.showToast({title: '上传失败', icon: 'none', duration: 2000})
        reject(err)
      }
    })
  })
}

export function getAddressListApi() {
  return request({
    url: 'address/query'
  })
}

export function queryOneAddressApi(id) {
  return request({
    url: 'address/queryOne',
    data: {
      id
    }
  })
}

export function submitOrderApi(data) {
  return request({
    url: 'order/add',
    method: 'post',
    data
  })
}

export function deleOneOrderApi(id) {
  return request({
    url: 'order/del',
    method: 'post',
    data: {
      id
    }
  })
}

export function takeOrderApi(data) {
  return request({
    url: 'order/takeOrder',
    method: 'post',
    data
  })
}

export function updateOrderApi(data) {
  return request({
    url: 'order/update',
    method: 'post',
    data
  })
}

export const getReceiveOrdersApi = () => request({
  url: 'order/query',
  data: {
    state: 0,
    isHome: true
  }
})

export const submitCertificationAPI = data => request({
  url: 'approve/submit',
  method: 'post',
  data
})

export const updateCertificationAPI = data => request({
  url: 'approve/update',
  method: 'post',
  data
})


export const getCertificationDataAPI = () => request({
  url: 'approve/queryOne',
})
